package com.hand.lwf24378.materials.service;

import com.github.pagehelper.PageHelper;
import com.hand.lwf24378.materials.domain.Materials;
import com.hand.lwf24378.materials.dto.SelectOrderHelper;
import com.hand.lwf24378.materials.exception.EnumExceptions;
import com.hand.lwf24378.materials.exception.MaterialsException;
import com.hand.lwf24378.materials.mapper.MaterialsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : WaveLee
 * @date : 2019/7/19 15:18
 * @description :
 */
@Service
public class MaterialsService {
    @Autowired
    MaterialsMapper materialsMapper;

    public Materials add(Materials materials){
        // 验证是否存在
        if (materialsMapper.getById(materials.getItemId()) != null
                || materialsMapper.getByCode(materials.getItemCode()) != null) {
            throw new MaterialsException(EnumExceptions.ADD_FAILED_DUPLICATE);
        }
        //构造编码
        long newCodeSuffix = Long.valueOf(materialsMapper.getMaxCode().substring(4))+1L;
        long maxCode = 9999999999L;
        //判断自增编码上限
        if (newCodeSuffix > maxCode){
            throw new MaterialsException(EnumExceptions.ADD_FAILED_MAX);
        }
        StringBuilder newCode = new StringBuilder("ITEM");
        //补全编码
        int max = 10;
        for(int i = 0;i<max-Long.toString(newCodeSuffix).length();i++){
            newCode.append("0");
        }
        newCode.append(newCodeSuffix);
        materials.setItemCode(newCode.toString());

        materialsMapper.add(materials);
        return materials;
    }

    public Materials update(Materials materials){
        // 验证是否存在
        if (materialsMapper.getById(materials.getItemId()) == null) {
            throw new MaterialsException(EnumExceptions.UPDATE_FAILED_NOT_EXIST);
        }
        Materials old = materialsMapper.getById(materials.getItemId());
        if (!old.getObjectVersionNumber().equals(materials.getObjectVersionNumber())){
            throw new MaterialsException(EnumExceptions.UPDATE_FAILED_OVN_CHANGED);
        }

        //数据更新
        Date current =new Date();
        materials.setLastUpdateDate(current);
        materials.setLastUpdatedBy(7L);
        materials.setObjectVersionNumber(materials.getObjectVersionNumber()+1);

        materialsMapper.update(materials);
        return materials;
    }

    public void delete(Long itemId){
        // 验证是否存在
        if (materialsMapper.getById(itemId) == null) {
            throw new MaterialsException(EnumExceptions.DELETE_FAILED_NOT_EXIST);
        }
        materialsMapper.delete(itemId);
    }

    public Materials getById(Long itemId){
        // 验证是否存在
        if (materialsMapper.getById(itemId) == null) {
            throw new MaterialsException(EnumExceptions.SELECT_FAILED_NOT_EXIST);
        }
        return materialsMapper.getById(itemId);
    }

    public List<Materials> getAll(){
        return materialsMapper.getAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteInBatch(Long[] itemIds){
        System.out.print(Arrays.toString(itemIds));
        materialsMapper.deleteInBatch(itemIds);
    }

    public List<Materials> selectItem(SelectOrderHelper selectOrderHelper){
        PageHelper.startPage(selectOrderHelper.getPage(), selectOrderHelper.getRows());
        return materialsMapper.selectItem(selectOrderHelper);
    }

}
